package dev

import (
	"fmt"
	"time"

	"jgt-wallet/src/bll"
	"jgt-wallet/src/context"
	"jgt-wallet/src/schema"

	"github.com/spf13/viper"
)

type WechatApp struct {
	WechatAppBll *bll.WechatApp `inject:""`
	OrderBll     *bll.Order     `inject:""`
	orderId      string
}

// 微信应用支付接口
func (a *WechatApp) Pay(ctx *context.Context) error {
	var (
		key    string
		param  = new(schema.WechatAppPayParams)
		err    error
		result map[string]string
	)

	a.orderId = fmt.Sprintf("%d", time.Now().Unix())
	param.OrderID = a.orderId
	param.Price = ctx.ParamInt64("price")
	param.Describe = fmt.Sprintf("测试请求[%d]", time.Now().Unix())
	param.Tag = "测试"

	key = "test"
	param.Url = fmt.Sprintf("%s/api/v1/public/wechat/callback/app/%s", viper.GetString("web_addr"), key)

	result, err = a.WechatAppBll.Pay(key, param)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}
	return ctx.ResSuccess(result)
}

// 支付宝应用同步回调接口
func (a *WechatApp) Check(ctx *context.Context) error {
	var (
		result *schema.OrderQueryResult
		err    error
	)
	orderId := ctx.Param("orderId")
	result, err = a.OrderBll.GetOrderResult(orderId)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	return ctx.ResSuccess(result.Result)
}
